package com.cskaoyan.service.goods;

import com.cskaoyan.bean.goods.Comment;
import com.cskaoyan.bean.goods.CommentExample;
import com.cskaoyan.bo.goods.GoodsPageAndSortBo;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.vo.goods.CommentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description:
 * author:houzhitao@139.com
 */
@Service
public class CommentServiceImpl implements CommentService{

    @Autowired
    CommentMapper commentMapper;

    @Override
    public CommentVo queryComment(GoodsPageAndSortBo pageAndSortBo) {
        //分页和排序
        PageHelper.startPage(pageAndSortBo.getPage(),pageAndSortBo.getLimit());//参数为起始页码和每页显示条数

        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(pageAndSortBo.getSort()+" "+pageAndSortBo.getOrder());
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Comment> items = commentMapper.selectByExample(commentExample);

        //查询总的条数
        PageInfo<Comment> commentPageInfo = new PageInfo<>(items);
        long total = commentPageInfo.getTotal();

        //将上边查询数据结果放入返回的结果中
        CommentVo commentVo = new CommentVo();
        commentVo.setTotal(total);
        commentVo.setItems(items);
        return commentVo;
    }

    @Override
    public Integer deleteComment(Comment comment) {

        //删除为浅删除，即只修改delete状态
        Comment comment1 = new Comment();
        comment1.setId(comment.getId());
        comment1.setDeleted(true);
        int i = commentMapper.updateByPrimaryKeySelective(comment1);
        return i;
    }
}
